<link href="<?php echo base_url('theme/css/frontend/company-add.css'); ?>" rel="stylesheet">
<link href="<?php echo base_url('theme/css/frontend/account-manager.css'); ?>" rel="stylesheet">
<script src="<?php echo base_url('theme/js/frontend/jquery.ajax.upload.js'); ?>"></script>
<link rel="stylesheet" href="<?php echo base_url('theme/css/frontend/jquery.Jcrop.min.css'); ?>" type="text/css" />
<script type="text/javascript">
    $(document).ready(function() {
        $('input[id^=file]').each(function() {
            var upload = new AjaxUpload($(this), {
                //upload script
                action: '/main/ajaxUpload',
                onSubmit: function(file, extension) {
                    id = $(this)[0]['_button']['id'];
                    //check file extension
                    if (!(extension && /^(jpg|png|jpeg|gif)$/.test(extension))) {
                        // extension is not allowed
                        $("<span class='error'>Error: Not a valid file extension</span>").appendTo($('div[id=' + id + '][name=error]'));
                        // cancel upload
                        return false;
                    } else {
                        // get rid of error
                        $('.error').hide();
                    }
                    //send the data
                    upload.setData({
                        'file': file
                    });
                },
                onComplete: function(file, response) {
                    //This lower portion gets the error message from upload.php file and appends it to our specifed error message block
                    //find the div in the iFrame and append to error message
                    if (response.indexOf('div') >= 0) {
                        $('div[id=' + id + '][name=error]').html(response);
                    } else {
                        file = jQuery.parseJSON(response);
                        $('div[id=' + id + '][name=thumbnails]').html('<img style="max-width: 300px;" src="' + file.link + '" />');
                        $('input[name=fileUpload]').val(file.file);
                    }
                }
            });
        });
    });

    function submitForm(i) {
        $.ajax({
            type: "POST",
            url: "<?php echo base_url('main/uploadFile/'); ?>/" + i,
            data: {
                number: i,
                image: $('input[name=fileUpload]').val(),
                url: $('#url_' + i).val(),
                title1: $('#title1_' + i).val(),
                title2: $('#title2_' + i).val(),
                title3: $('#title3_' + i).val(),
            },
            success: function() {
                location.href = "<?php echo base_url('main/general'); ?>";
            }
        });
    }

    function banner(i) {
        $('.tr-table-content').each(function() {
            tag = $(this).attr('tag');
            if (tag == i) {
                $(this).show();
                $('.banner-title[tag=' + tag + ']').addClass('banner-active');
            } else {
                $(this).hide();
                $('.banner-title[tag=' + tag + ']').removeClass('banner-active');
            }
        });
    }
    
    function smallBanner(i) {
        $('.tr-small-table-content').each(function() {
            tag = $(this).attr('tag');
            if (tag == i) {
                $(this).show();
                $('.small-banner-title[tag=' + tag + ']').addClass('banner-active');
            } else {
                $(this).hide();
                $('.small-banner-title[tag=' + tag + ']').removeClass('banner-active');
            }
        });
    }
</script>
<div class="management-container booth-container" style="margin-top: 40px;">
    <h3 style="color:black;">Vị trí quảng cáo</h3>
    <hr class="hr" />

    <div class="management-wrapper">
        <div style="min-height:65px;">
            <button class="btn btn-primary btn-event" style="width:200px" type="button">
                Top Banner
            </button>
        </div>
        <hr class="hr" />
        <div class="management-block-banner">
            <table class="banner-table">
                <tr>
                    <th>
                        <span tag="1" class="banner-title banner-active" onclick="banner('1');"><img src="<?php echo base_url('theme/images/frontend/square.png'); ?>" />Banner 1</span>
                        <span tag="2" class="banner-title" onclick="banner('2');"><img src="<?php echo base_url('theme/images/frontend/square.png'); ?>" />Banner 2</span>
                        <span tag="3" class="banner-title" onclick="banner('3');"><img src="<?php echo base_url('theme/images/frontend/square.png'); ?>" />Banner 3</span>
                        <span tag="4" class="banner-title" onclick="banner('4');"><img src="<?php echo base_url('theme/images/frontend/square.png'); ?>" />Banner 4</span>
                        <span tag="5" class="banner-title" onclick="banner('5');"><img src="<?php echo base_url('theme/images/frontend/square.png'); ?>" />Banner 5</span>
                        <span tag="6" class="banner-title" onclick="banner('6');"><img src="<?php echo base_url('theme/images/frontend/square.png'); ?>" />Banner 6</span>
                    </th>
                </tr>
                <?php foreach ($types as $type => $name): ?>
                    <tr class="tr-table-content" <?php if ($type != 1) { ?> style="display:none" <?php } ?> tag="<?php echo $type; ?>">
                        <td>
                            <div class="banner-table-content">
                                <div class="">
                                    <form class="form-horizontal" id="submit_<?php echo $type ?>" action="<?php echo base_url('main/uploadFile/' . $type); ?>" method="post">
                                        <div class="left">
                                            <span style="font-weight:bold">Vị trí up banner</span>
                                            <div id="file_<?php echo $type ?>" name="thumbnails">
                                                <?php if (isset($banner[$type - 1]) && $banner[$type - 1]['image'] != "") { ?>
                                                    <img style="max-width: 300px;" src="<?php echo base_url('/upload/general/') . '/' . $banner[$type - 1]['image']; ?>" />
                                                <?php } else { ?>
                                                    <img style="max-width: 300px;" src="<?php echo base_url('/theme/images/700x430.jpg'); ?>" />
                                                <?php } ?>
                                            </div>
                                            <div id="file_<?php echo $type ?>" name="error" style="color:red"></div>
                                            <div id="<?php echo $type; ?>_append"></div>
                                        </div>
                                        <div class="right">
                                            <div class="control-group">
                                                <label for="url" class="control-label-event">Nhập URL</label>
                                                <div class="controls-event">
                                                    <input type=text name="url" id="url_<?php echo $type ?>" class="input-xxlarge-event" value="<?php if (isset($banner[$type - 1]) && $banner[$type - 1]['url'] != "") echo $banner[$type - 1]['url']; ?>">
                                                </div>
                                            </div>
                                            <div class="control-group">
                                                <label for="url" class="control-label-event">Tiêu đề 1</label>
                                                <div class="controls-event">
                                                    <input type=text name="url" id="title1_<?php echo $type ?>" class="input-xxlarge-event" value="<?php if (isset($banner[$type - 1]) && $banner[$type - 1]['title1'] != "") echo $banner[$type - 1]['title1']; ?>">
                                                </div>
                                            </div>
                                            <div class="control-group">
                                                <label for="url" class="control-label-event">Tiêu đề 2</label>
                                                <div class="controls-event">
                                                    <input type=text name="url" id="title2_<?php echo $type ?>" class="input-xxlarge-event" value="<?php if (isset($banner[$type - 1]) && $banner[$type - 1]['title2'] != "") echo $banner[$type - 1]['title2']; ?>">
                                                </div>
                                            </div>
                                            <div class="control-group">
                                                <label for="url" class="control-label-event">Tiêu đề 3</label>
                                                <div class="controls-event">
                                                    <input type=text name="url" id="title3_<?php echo $type ?>" class="input-xxlarge-event" value="<?php if (isset($banner[$type - 1]) && $banner[$type - 1]['title3'] != "") echo $banner[$type - 1]['title3']; ?>">
                                                </div>
                                            </div>
                                            <form method="post" id="upload_<?php echo $type ?>" class="form-horizontal" action=<?php echo base_url('main/uploadFile/' . $type); ?> enctype=multipart/form-data > 
                                                <div class=control-group>
                                                    <label for="<?php echo $type ?>" class="control-label-event">Hình ảnh</label>
                                                    <div class="controls-event">
                                                        <input type="hidden" id="MAX_FILE_SIZE" name="MAX_FILE_SIZE" value="600000" />
                                                        <input type="file" id="file_<?php echo $type ?>" name="file_<?php echo $type ?>[]" multiple="multiple" />
                                                    </div>
                                                </div>
                                                <div class="submitbutton" style="display:none">
                                                    <button type="submit">Upload Files</button>
                                                </div>
                                            </form>
                                        </div>
                                        <div class="button">
                                            <div class="control-group">
                                                <div class=controls style="margin:0!important">
                                                    <input type="hidden" id="fileUpload" name="fileUpload" />
                                                    <input type="hidden" id="linkUpload" name="linkUpload" />
                                                    <input type="hidden" name="banner_id" value="<?php echo $type ?>" />
                                                    <button class="btn btn-primary" type=submit value="Lưu thay đổi" style="float: right;margin-bottom: 5px;" onclick="submitForm('<?php echo $type; ?>')">Đăng ký</button>
                                                </div>
                                            </div>
                                        </div>
                                    </form>
                                </div>
                            </div>
                        </td>
                    </tr>
                <?php endforeach; ?>
                <tr>
                    <th style="padding-left:245px">
                        <span tag="7" class="small-banner-title banner-active" onclick="smallBanner('7');"><img src="<?php echo base_url('theme/images/frontend/square.png'); ?>" />Banner 1</span>
                        <span tag="8" class="small-banner-title" onclick="smallBanner('8');"><img src="<?php echo base_url('theme/images/frontend/square.png'); ?>" />Banner 2</span>
                        <span tag="9" class="small-banner-title" onclick="smallBanner('9');"><img src="<?php echo base_url('theme/images/frontend/square.png'); ?>" />Banner 3</span>
                    </th>
                </tr>
                <?php foreach ($smalls as $small => $name): ?>    
                    <tr class="tr-small-table-content" <?php if ($small != 7) { ?> style="display:none" <?php } ?> tag="<?php echo $small; ?>">
                        <td>
                            <div class="banner-table-content">
                                <div class="">
                                    <form class="form-horizontal" id="submit_<?php echo $small ?>" action="<?php echo base_url('main/uploadFile/' . $small); ?>" method="post">
                                        <div class="small-left">
                                            <span style="font-weight:bold">Vị trí up banner</span>
                                            <div id="file_<?php echo $small ?>" name="thumbnails">
                                                <?php if (isset($banner[$small - 1]) && $banner[$small - 1]['image'] != "") { ?>
                                                    <img style="max-width: 300px;" src="<?php echo base_url('/upload/general/') . '/' . $banner[$small - 1]['image']; ?>" />
                                                <?php } else { ?>
                                                    <img style="max-width: 300px;" src="<?php echo base_url('/theme/images/700x430.jpg'); ?>" />
                                                <?php } ?>
                                            </div>
                                            <div id="file_<?php echo $small ?>" name="error" style="color:red"></div>
                                            <div id="<?php echo $small; ?>_append"></div>
                                        </div>
                                        <div class="right">
                                            <div class="control-group">
                                                <label for="url" class="control-label-event">Nhập URL</label>
                                                <div class="controls-event">
                                                    <input type=text name="url" id="url_<?php echo $small ?>" class="input-xxlarge-event" value="<?php if (isset($banner[$small - 1]) && $banner[$small - 1]['url'] != "") echo $banner[$small - 1]['url']; ?>">
                                                </div>
                                            </div>
                                            <form method="post" id="upload_<?php echo $small ?>" class="form-horizontal" action=<?php echo base_url('main/uploadFile/' . $small); ?> enctype=multipart/form-data > 
                                                <div class=control-group>
                                                    <label for="<?php echo $small ?>" class="control-label-event">Hình ảnh</label>
                                                    <div class="controls-event">
                                                        <input type="hidden" id="MAX_FILE_SIZE" name="MAX_FILE_SIZE" value="600000" />
                                                        <input type="file" id="file_<?php echo $small ?>" name="file_<?php echo $small ?>[]" multiple="multiple" />
                                                    </div>
                                                </div>
                                                <div class="submitbutton" style="display:none">
                                                    <button type="submit">Upload Files</button>
                                                </div>
                                            </form>
                                        </div>
                                        <div class="button">
                                            <div class="control-group">
                                                <div class=controls style="margin:0!important">
                                                    <input type="hidden" id="fileUpload" name="fileUpload" />
                                                    <input type="hidden" id="linkUpload" name="linkUpload" />
                                                    <input type="hidden" name="banner_id" value="<?php echo $small ?>" />
                                                    <button class="btn btn-primary" type=submit value="Lưu thay đổi" style="float: right;margin-bottom: 5px;" onclick="submitForm('<?php echo $small; ?>')">Đăng ký</button>
                                                </div>
                                            </div>
                                        </div>
                                    </form>
                                </div>
                            </div>
                        </td>
                    </tr>
                <?php endforeach; ?>
            </table>
        </div>
    </div>
</div>